//! File system system calls
//! SPDX-License-Identifier: MPL-2.0

const std = @import("std");
const SyscallReturn = @import("../return.zig").SyscallReturn;
const SyscallError = @import("../error.zig").SyscallError;
const Context = @import("../context.zig").Context;
const Fd = @import("../context.zig").Fd;
const VirtAddr = @import("../context.zig").VirtAddr;

// File system syscall implementations
pub const read = @import("read.zig");
pub const write = @import("write.zig");
pub const open = @import("open.zig");
pub const close = @import("close.zig");
pub const stat = @import("stat.zig");

/// Read from a file descriptor
pub fn sys_read(fd: Fd, buf_addr: VirtAddr, count: usize, ctx: *Context) SyscallReturn {
    return read.sys_read(fd, buf_addr, count, ctx);
}

/// Write to a file descriptor
pub fn sys_write(fd: Fd, buf_addr: VirtAddr, count: usize, ctx: *Context) SyscallReturn {
    return write.sys_write(fd, buf_addr, count, ctx);
}

/// Open a file
pub fn sys_open(pathname_addr: VirtAddr, flags: i32, mode: u32, ctx: *Context) SyscallReturn {
    return open.sys_open(pathname_addr, flags, mode, ctx);
}

/// Close a file descriptor
pub fn sys_close(fd: Fd, ctx: *Context) SyscallReturn {
    return close.sys_close(fd, ctx);
}

/// Get file status
pub fn sys_stat(pathname_addr: VirtAddr, statbuf_addr: VirtAddr, ctx: *Context) SyscallReturn {
    return stat.sys_stat(pathname_addr, statbuf_addr, ctx);
}

/// Get file status by file descriptor
pub fn sys_fstat(fd: Fd, statbuf_addr: VirtAddr, ctx: *Context) SyscallReturn {
    return stat.sys_fstat(fd, statbuf_addr, ctx);
}

/// Get file status (don't follow symlinks)
pub fn sys_lstat(pathname_addr: VirtAddr, statbuf_addr: VirtAddr, ctx: *Context) SyscallReturn {
    return stat.sys_lstat(pathname_addr, statbuf_addr, ctx);
}

/// Poll file descriptors
pub fn sys_poll(fds_addr: VirtAddr, nfds: u32, timeout: i32, ctx: *Context) SyscallReturn {
    _ = fds_addr;
    _ = nfds;
    _ = timeout;
    _ = ctx;
    // TODO: Implement poll
    return SyscallReturn.error_return(SyscallError.ENOSYS);
}

/// Seek in a file
pub fn sys_lseek(fd: Fd, offset: i64, whence: i32, ctx: *Context) SyscallReturn {
    _ = fd;
    _ = offset;
    _ = whence;
    _ = ctx;
    // TODO: Implement lseek
    return SyscallReturn.error_return(SyscallError.ENOSYS);
}